﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

using CE.Database;
using CE.Global;
using CE.User;
using CE.Job;

public partial class job_list : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["country"] != null)
            {
                JobStartBind();
            }
            else
            {
                Response.Redirect("job-list.aspx?country=MY");
            }

            if (Session[CE.Global.General.UserId] != null)
            {
                liLogin.Visible = false;
                liProfile.Visible = true;
                liApplication.Visible = true;
                liLogout.Visible = true;
                liChangePass.Visible = true;
            }
            
        }
    }
    private void JobStartBind()
    {
        string _country = Request.Params["country"].ToString();
        JobDataBind(_country);
        ClearSelectCSS();
        switch (_country)
        {
            case "HK":
                aHK.Attributes.Add("class", "select");
                break;
            case "ID":
                aID.Attributes.Add("class", "select");
                break;
            case "MY":
                aMY.Attributes.Add("class", "select");
                break;
            case "SG":
                aSG.Attributes.Add("class", "select");
                break;
            case "TH":
                aTH.Attributes.Add("class", "select");
                break;
        }
    }
    private void JobDataBind(string country)
    {
        GetActiveJobs(country);


        dlJob.DataSource = (PagedDataSource)Cache["ActiveJobs"];
        dlJob.DataBind();


        //if (dlJob.Items.Count == 0)
        //    pnlNoRecords.Visible = true;
        //else
        //    pnlNoRecords.Visible = false;

    }
    private void GetActiveJobs(string country)
    {
        string _sort = "CreatedDate";
        string _sortby = "desc";
        if (Request.Params["sortby"] != null)
        {
            if (Request.Params["sortby"].ToString() == "Job Title")
            {
                _sort = "JobTitle";
                _sortby = "asc";
                ddlSort.SelectedValue = "JobTitle";
            }
        }

        //MyDatabase _db = new MyDatabase();
        //DataTable _dtResult = new DataTable();
        //string _spName = "[sprocJobSelectSingleItemListByCountry]";
        //SortedList _spara = new SortedList();
        //_spara.Add("@Country", country);
        //_dtResult = _db.GetDataTableBySP(_spName, _spara);

        MyDatabase _db = new MyDatabase();
        DataSet _dsResult = new DataSet();
        string _spName = "[sprocJobSelectSingleItemListByCountryIsActive]";
        SortedList _spara = new SortedList();
        _spara.Add("@Country", country);
        _spara.Add("@Sort", _sort);
        _spara.Add("@SortBy", _sortby);
        _dsResult = _db.GetDataSet(_spName, _spara);

        PagedDataSource pageds = new PagedDataSource();
        pageds.DataSource = _dsResult.Tables[0].DefaultView;
        pageds.AllowPaging = true;
        pageds.PageSize = 10;

        int curpage = 0;

        if (Request.QueryString["page"] != null)
        {
            curpage = Convert.ToInt32(Request.QueryString["page"]);
        }
        else
        {
            curpage = 1;
        }

        pageds.CurrentPageIndex = curpage - 1;

        if (curpage == 1 && pageds.DataSourceCount > pageds.PageSize)
        {
            lblCurrpage.Text = "Pages: 1";
            lblCurrpageTop.Text = "Pages: 1";
        }
        else if (pageds.DataSourceCount == 0)
        {
            lblCurrpage.Text = "No data to display.";
            lblCurrpageTop.Text = "";
        }
        else if (curpage > 1 && pageds.DataSourceCount > pageds.PageSize)
        {
            lblCurrpage.Text = "Pages: <a href='job-list.aspx?country=" + Request.Params["Country"].ToString() + "&sortby=" + ddlSort.SelectedItem.Text + "&page=1#list-contet'>1</a>";
            lblCurrpageTop.Text = "Pages: <a href='job-list.aspx?country=" + Request.Params["Country"].ToString() + "&sortby=" + ddlSort.SelectedItem.Text + "&page=1#list-contet'>1</a>";
        }
        for (int i = 2; i <= pageds.PageCount; i++)
        {
            if (i == curpage)
            {
                lblCurrpage.Text = lblCurrpage.Text + " " + i.ToString();
                lblCurrpageTop.Text = lblCurrpageTop.Text + " " + i.ToString();
            }
            else
            {
                lblCurrpage.Text = lblCurrpage.Text + " <a href='job-list.aspx?country=" + Request.Params["Country"].ToString() + "&page=" + i.ToString() + "&sortby=" + ddlSort.SelectedItem.Text + "#list-contet'>" + i.ToString() + "</a>";
                lblCurrpageTop.Text = lblCurrpageTop.Text + " <a href='job-list.aspx?country=" + Request.Params["Country"].ToString() + "&page=" + i.ToString() + "&sortby=" + ddlSort.SelectedItem.Text + "#list-contet'>" + i.ToString() + "</a>";
            }
        }

        Cache["ActiveJobs"] = pageds;
    }
    private void ClearSelectCSS()
    {
        aHK.Attributes.Add("class", "");
        aID.Attributes.Add("class", "");
        aMY.Attributes.Add("class", "");
        aSG.Attributes.Add("class", "");
        aTH.Attributes.Add("class", "");
    }
    protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e)
    {
        string _redirect = "job-list.aspx?country=" + Request.Params["Country"].ToString();
        if (Request.Params["page"] != null)
            _redirect += "&page=" + Request.Params["page"].ToString();
        _redirect += "&sortby=" + ddlSort.SelectedItem.Text + "#list-contet";

        Response.Redirect(_redirect);
    }
}

